Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The yn package is a simple yet powerful utility for parsing yes/no like values. It can be used to convert various types of user inputs into boolean values. This is particularly useful for handling configurations, environment variables, or any input that requires a boolean interpretation.
Basic parsing
This demonstrates basic parsing of yes/no like values into booleans. It shows how 'y', 'NO', true, and false are interpreted.
const yn = require('yn');
console.log(yn('y')); // true
console.log(yn('NO')); // false
console.log(yn(true)); // true
console.log(yn(false)); // false
Parsing with options
This shows how to use options to control the parsing behavior, such as setting a default value or enabling lenient parsing for more flexible yes values.
const yn = require('yn');
console.log(yn('y', { default: false })); // true
console.log(yn('abracadabra', { default: false })); // false
console.log(yn(null, { default: true })); // true
console.log(yn('yes', { lenient: true })); // true
Handling numeric truthy/falsy values
Demonstrates yn's ability to interpret '1' as true and '0' as false, which is useful for numeric representations of boolean values.
const yn = require('yn');
console.log(yn('1')); // true
console.log(yn('0')); // false
Similar to yn, the boolean package is designed to convert various types of values into booleans. However, it focuses more on a broader range of truthy and falsy values without the specific emphasis on yes/no strings.
This package offers functionality similar to yn by converting strings to boolean values. It differs in its approach to parsing and the range of strings it considers to be true or false.
Parse yes/no like values
Useful for validating answers of a CLI prompt.
The following case-insensitive values are recognized:
'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0
Enable lenient mode to gracefully handle typos.
$ npm install yn
const yn = require('yn');
yn('y');
//=> true
yn('NO');
//=> false
yn(true);
//=> true
yn('abomasum');
//=> null
yn('abomasum', {default: false});
//=> false
yn('mo', {lenient: true});
//=> false
Unrecognized values return null
.
Type: any
Value that should be converted.
Type: Object
Type: boolean
Default: false
Use a key distance-based score to leniently accept typos of yes
and no
.
Type: boolean
Default: null
Default value if no match was found.
MIT © Sindre Sorhus
FAQs
Parse yes/no like values
We found that yn demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.